package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.sqlite.entities.ReEventDataEntity;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: OperateB2cDBService
 * @Description: 事件上传B2CReport
 * @author Xiaofeng Feng
 * @date 2015年8月14日 下午2:15:18 
 */
public class OperateB2cDBService extends BaseSqlImp {
	
	public OperateB2cDBService(Context context) {
		super(context);
	}
	
	/**
	 * @Title: insert
	 * @Description: insert data to table
	 * @param @table t_table_eventservice
	 * @param @param entity
	 * @param @return 设定文件
	 * @return long 返回类型
	 * @throws
	 */
	public  long insert(ReEventDataEntity entity) {
		long rowId = 0;
		try {
			mDbUtils.save(entity);
			rowId = mDbUtils.count(ReEventDataEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		} 
		return rowId;
	}
	
	/**
	 * @Title: insert
	 * @Description: insert data to table
	 * @param @table t_table_eventservice
	 * @param @param list
	 * @param @return 设定文件
	 * @return long 返回类型
	 * @throws
	 */
	public long insert(List<ReEventDataEntity> list) {
		long rowId = 0;
		try {
			mDbUtils.saveAll(list);
			rowId = mDbUtils.count(ReEventDataEntity.class);
//			for (ReEventDataEntity entity : list) {
//				rowId = super.insert(entity);
//				if (rowId == -1) {
//					break;
//				}
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rowId;
	}
	
	/**
	 * @Title: delete @Description: delete data to table @param @table
	 * t_table_eventservice @param @return 设定文件 @return void 返回类型 @throws
	 */
	public void delete(String id) {
		try {
			mDbUtils.delete(ReEventDataEntity.class, WhereBuilder.b("_id","=",Integer.parseInt(id)));
		} catch (DbException e) {
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
//		super.delete("_id=? ", new String[] { id });
	}
	
	
	/**
	 * @Title: deleteAll
	 * @Description: deleteAll data to table
	 * @param @table t_table_eventservice
	 * @param @return 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public void deleteAll(List<ReEventDataEntity> eventList, boolean isGetAll) {
		try {
//			String orderBy = null;
//			if (!isGetAll) {
//				orderBy = ReEventDataEntity.CREATE_DATE + " desc ";
//			}
			if (eventList == null) {
//				eventList = getAll(null, null, null, null, null, orderBy, "0,50");
				Selector selector = Selector.from(ReEventDataEntity.class);
				if(!isGetAll){
					selector = selector.orderBy(ReEventDataEntity.CREATE_DATE,true);
				}
				selector = selector.limit(50);
				eventList = mDbUtils.findAll(selector);
			}
//			for (ReEventDataEntity entity : eventList) {
//				if (entity.getId() != null)
//					delete(entity.getId().toString());
//			}
			mDbUtils.deleteAll(eventList);
		} catch (Exception e) {
			e.printStackTrace();
			eventList = new ArrayList<ReEventDataEntity>();
		}  
	}
	
	/**
	 * @Title: getList
	 * @Description: getList data to table
	 * @param @table t_table_eventservice
	 * @param @return 设定文件
	 * @return list 返回类型
	 * @throws
	 */
	public List<ReEventDataEntity> getList(boolean isGetAll) {
		List<ReEventDataEntity> list = new ArrayList<ReEventDataEntity>();
		try {
//			String orderBy = null;
//			if (!isGetAll) {
//				orderBy = ReEventDataEntity.CREATE_DATE + " desc ";
//			}
			Selector selector = Selector.from(ReEventDataEntity.class);
			if(!isGetAll){
				selector = selector.orderBy(ReEventDataEntity.CREATE_DATE,true);
			}
			selector = selector.limit(50);
//			list = getAll(null, null, null, null, null, orderBy, "0,50");
			list = mDbUtils.findAll(selector);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return list;
	}
	
	
	/**
	 * @Title: getCount
	 * @Description: getCount data to table
	 * @param @table t_table_eventservice
	 * @return Integer 返回类型
	 * @throws
	 */
	public Integer getCount() {
//		String sql = "select * from " + ReEventDataEntity.TABLE_NAME;
//		return super.getCount(sql,null);
		try {
			return (int)mDbUtils.count(ReEventDataEntity.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
